package hong.jun.pet_home.controller.Registration_login;

import hong.jun.basic.util.AjaxResult;
import hong.jun.pet_home.domain.util.EmailMessage;
import hong.jun.pet_home.domain.util.EmailMessageType;
import hong.jun.pet_home.domain.org.Shop;
import hong.jun.pet_home.domain.util.SendEmailUtil;
import hong.jun.pet_home.service.org.IShopService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/SHOP")
@Api(tags = "Shop_Login_Register_Controller", description = "Shop_Login_Register_Controller | 商铺登录注册接口")
public class Shop_Login_Register_Controller {
    @Autowired
    private IShopService shopService;
    @Autowired //用来发送邮件的工具类
    private SendEmailUtil sendEmailUtil;

    @PostMapping("/register")
    public AjaxResult shopregister(@RequestBody Shop shop) {
        AjaxResult ajaxResult = shopService.shopregister(shop);
        if (ajaxResult.getMessage() == "操作成功!") {
            try {//持久化成功才发邮件
                EmailMessage emailMessage = EmailMessageType.SHOP_SETTLED_NOTTICE.shopEvent(shop);
                sendEmailUtil.sendEmail(emailMessage);
            } finally {//邮件无论发送成功都返会成功 用户等待审核
                return AjaxResult.me();
            }
        } else {
            return ajaxResult;
        }
    }

}
